//
//  UIView+Animation.m
//  MFWSdk
//
//  Created by BigPolarBear on 13-8-28.
//  Copyright (c) 2013年 Mafengwo Inc. All rights reserved.
//

#import "UIView+Animation.h"

@implementation UIView (Animation)



-(void)startSpinWithNeedStopBlock:(BOOL (^)(void))needStopBlock
{
    
    UIView* aView = self;
    
    UIViewAnimationOptions options = UIViewAnimationOptionCurveEaseIn;
    
    // this spin completes 360 degrees every 2 seconds
    [UIView animateWithDuration: 0.3f
                          delay: 0.0f
                        options: options|UIViewAnimationOptionAllowUserInteraction
                     animations: ^{
                         aView.transform = CGAffineTransformRotate(aView.transform, M_PI / 2);
                     }
                     completion: ^(BOOL finished) {
                         if (finished) {
                             BOOL needStop = YES;
                             if(needStopBlock)
                             {
                                 needStop = needStopBlock();
                             }
                             
                             if (needStop) {
                                 // 最后一圈
                                 [self stopSpinView:aView];
                             }
                             else{
                                 [self resumeSpinView:aView needStopBlock:needStopBlock];
                             }
                         }
                     }];
}

-(void)resumeSpinView:(UIView*)aView needStopBlock:(BOOL (^)(void))needStopBlock
{
    [UIView animateWithDuration: 0.3f
                          delay: 0.0f
                        options: UIViewAnimationOptionCurveLinear|UIViewAnimationOptionAllowUserInteraction
                     animations: ^{
                         aView.transform = CGAffineTransformRotate(aView.transform, M_PI / 2);
                     }
                     completion:^(BOOL finished) {
                         if (finished) {
                             BOOL needStop = YES;
                             if(needStopBlock)
                             {
                                 needStop = needStopBlock();
                             }
                             
                             if (needStop) {
                                 [self stopSpinView:aView];
                             }else{
                                 [self resumeSpinView:aView needStopBlock:needStopBlock];
                             }
                         }
                     }];
}

-(void)stopSpinView:(UIView*)aView
{
    [UIView animateWithDuration: 0.3f
                          delay: 0.0f
                        options: UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionAllowUserInteraction
                     animations: ^{
                         aView.transform = CGAffineTransformRotate(aView.transform, M_PI / 2);
                     }
                     completion: nil];
}


@end
